All files / src/app/admin/api-docs/[category]/[endpoint] page.tsx

0% Statements 0/355
100% Branches 0/0
0% Functions 0/1
0% Lines 0/355

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
"use client";

import { useParams } from "next/navigation";
import { useMemo, useState } from "react";
import Link from "next/link";
import { apiRegistry } from "@/lib/api-docs/registry";
import {
  getMethodColor,
  generateCurl,
  generateTypescript,
  generateJavascript,
  generatePython} from "@/lib/api-docs/executor";
import { ApiTester } from "@/components/features/admin/api-docs";
import { ApiTestRequest } from "@/types/api-docs";

type CodeTab = "curl" | "typescript" | "javascript" | "python";

export default function EndpointPage() {
  const params = useParams();
  const categoryId = params.category as string;
  const endpointId = params.endpoint as string;

  const [activeCodeTab, setActiveCodeTab] = useState<CodeTab>("curl");
  const [copiedCode, setCopiedCode] = useState(false);

  const { category, endpoint } = useMemo(() => {
    const cat = apiRegistry.categories.find((c) => c.id === categoryId);
    const ep = cat?.endpoints.find((e) => e.id === endpointId);
    return { category: cat, endpoint: ep };
  }, [categoryId, endpointId]);

  // Create a mock request for code generation
  const mockRequest: ApiTestRequest | null = useMemo(() => {
    if (!endpoint) return null;
    return {
      endpoint,
      pathParams: {},
      queryParams: {},
      headers: {},
      body: endpoint.requestBody?.example,
      useAuth: endpoint.requiresAuth};
  }, [endpoint]);

  const codeSnippets = useMemo(() => {
    if (!mockRequest) return { curl: "", typescript: "", javascript: "", python: "" };
    return {
      curl: generateCurl(mockRequest),
      typescript: generateTypescript(mockRequest),
      javascript: generateJavascript(mockRequest),
      python: generatePython(mockRequest)};
  }, [mockRequest]);

  const copyCode = async (code: string) => {
    await navigator.clipboard.writeText(code);
    setCopiedCode(true);
    setTimeout(() => setCopiedCode(false), 2000);
  };

  if (!category || !endpoint) {
    return (
      <div className="min-h-screen bg-gray-50 dark:bg-gray-900 p-8">
        <div className="max-w-4xl mx-auto text-center">
          <h1 className="text-2xl font-bold text-gray-900 dark:text-white mb-4">
            Endpoint Not Found
          </h1>
          <p className="text-gray-600 dark:text-gray-400 mb-6">
            The endpoint &quot;{endpointId}&quot; does not exist in category &quot;{categoryId}&quot;.
          </p>
          <Link
            href="/admin/api-docs"
            className="text-blue-600 hover:text-blue-800 dark:text-blue-400"
          >
            &larr; Back to API Documentation
          </Link>
        </div>
      </div>
    );
  }

  return (
    <div className="min-h-screen bg-gray-50 dark:bg-gray-900">
      {/* Header */}
      <div className="bg-white dark:bg-gray-800 border-b border-gray-200 dark:border-gray-700">
        <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-6">
          <div className="flex items-center gap-2 text-sm text-gray-500 dark:text-gray-400 mb-4">
            <Link href="/admin/api-docs" className="hover:text-blue-600">
              API Documentation
            </Link>
            <span>/</span>
            <Link href={`/admin/api-docs/${categoryId}`} className="hover:text-blue-600">
              {category.name}
            </Link>
            <span>/</span>
            <span className="text-gray-900 dark:text-white">{endpoint.summary}</span>
          </div>

          <div className="flex items-center gap-4 mb-4">
            <span
              className={`px-3 py-1 text-sm font-bold rounded ${getMethodColor(endpoint.method)}`}
            >
              {endpoint.method}
            </span>
            <code className="text-lg font-mono text-gray-700 dark:text-gray-300">
              {endpoint.path}
            </code>
          </div>

          <h1 className="text-2xl font-bold text-gray-900 dark:text-white">
            {endpoint.summary}
          </h1>
          {endpoint.description && (
            <p className="mt-2 text-gray-600 dark:text-gray-400">{endpoint.description}</p>
          )}

          <div className="mt-4 flex items-center gap-3">
            {endpoint.requiresAuth && (
              <span className="px-3 py-1 text-sm bg-yellow-100 dark:bg-yellow-900/30 text-yellow-700 dark:text-yellow-300 rounded-full">
                Authentication Required
              </span>
            )}
            {endpoint.adminOnly && (
              <span className="px-3 py-1 text-sm bg-red-100 dark:bg-red-900/30 text-red-700 dark:text-red-300 rounded-full">
                Admin Only
              </span>
            )}
            {endpoint.tags?.map((tag) => (
              <span
                key={tag}
                className="px-3 py-1 text-sm bg-gray-100 dark:bg-gray-700 text-gray-700 dark:text-gray-300 rounded-full"
              >
                {tag}
              </span>
            ))}
          </div>
        </div>
      </div>

      <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
        <div className="grid grid-cols-1 xl:grid-cols-3 gap-8">
          {/* Left Column - Documentation */}
          <div className="xl:col-span-2 space-y-8">
            {/* Parameters */}
            {endpoint.parameters && endpoint.parameters.length > 0 && (
              <section className="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 overflow-hidden">
                <div className="px-6 py-4 border-b border-gray-200 dark:border-gray-700">
                  <h2 className="text-lg font-semibold text-gray-900 dark:text-white">
                    Parameters
                  </h2>
                </div>
                <div className="overflow-x-auto">
                  <table className="min-w-full divide-y divide-gray-200 dark:divide-gray-700">
                    <thead className="bg-gray-50 dark:bg-gray-900">
                      <tr>
                        <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
                          Name
                        </th>
                        <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
                          Location
                        </th>
                        <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
                          Type
                        </th>
                        <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
                          Required
                        </th>
                        <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
                          Description
                        </th>
                      </tr>
                    </thead>
                    <tbody className="bg-white dark:bg-gray-800 divide-y divide-gray-200 dark:divide-gray-700">
                      {endpoint.parameters.map((param) => (
                        <tr key={param.name}>
                          <td className="px-6 py-4 whitespace-nowrap">
                            <code className="text-sm font-mono text-blue-600 dark:text-blue-400">
                              {param.name}
                            </code>
                          </td>
                          <td className="px-6 py-4 whitespace-nowrap">
                            <span className="text-sm text-gray-600 dark:text-gray-400 capitalize">
                              {param.location}
                            </span>
                          </td>
                          <td className="px-6 py-4 whitespace-nowrap">
                            <span className="text-sm text-gray-600 dark:text-gray-400">
                              {param.type}
                            </span>
                          </td>
                          <td className="px-6 py-4 whitespace-nowrap">
                            {param.required ? (
                              <span className="text-red-600 dark:text-red-400">Yes</span>
                            ) : (
                              <span className="text-gray-400">No</span>
                            )}
                          </td>
                          <td className="px-6 py-4">
                            <span className="text-sm text-gray-600 dark:text-gray-400">
                              {param.description}
                            </span>
                          </td>
                        </tr>
                      ))}
                    </tbody>
                  </table>
                </div>
              </section>
            )}

            {/* Request Body */}
            {endpoint.requestBody && (
              <section className="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 overflow-hidden">
                <div className="px-6 py-4 border-b border-gray-200 dark:border-gray-700">
                  <h2 className="text-lg font-semibold text-gray-900 dark:text-white">
                    Request Body
                  </h2>
                  <p className="text-sm text-gray-500 dark:text-gray-400 mt-1">
                    Content-Type: {endpoint.requestBody.contentType}
                  </p>
                </div>
                <div className="p-6">
                  <h3 className="text-sm font-medium text-gray-900 dark:text-white mb-3">
                    Fields
                  </h3>
                  <div className="space-y-3">
                    {endpoint.requestBody.fields.map((field) => (
                      <div
                        key={field.name}
                        className="flex items-start gap-4 p-3 bg-gray-50 dark:bg-gray-900 rounded-lg"
                      >
                        <code className="text-sm font-mono text-blue-600 dark:text-blue-400 whitespace-nowrap">
                          {field.name}
                        </code>
                        <span className="text-sm text-gray-500 dark:text-gray-400">
                          {field.type}
                        </span>
                        {field.required && (
                          <span className="text-xs text-red-600 dark:text-red-400">required</span>
                        )}
                        <span className="text-sm text-gray-600 dark:text-gray-400 flex-1">
                          {field.description}
                        </span>
                      </div>
                    ))}
                  </div>

                  {endpoint.requestBody.example && (
                    <div className="mt-6">
                      <h3 className="text-sm font-medium text-gray-900 dark:text-white mb-3">
                        Example
                      </h3>
                      <pre className="p-4 bg-gray-900 text-gray-100 rounded-lg overflow-x-auto text-sm">
                        {JSON.stringify(endpoint.requestBody.example, null, 2)}
                      </pre>
                    </div>
                  )}
                </div>
              </section>
            )}

            {/* Responses */}
            <section className="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 overflow-hidden">
              <div className="px-6 py-4 border-b border-gray-200 dark:border-gray-700">
                <h2 className="text-lg font-semibold text-gray-900 dark:text-white">
                  Responses
                </h2>
              </div>
              <div className="divide-y divide-gray-200 dark:divide-gray-700">
                {endpoint.responses.map((response) => (
                  <div key={response.status} className="p-6">
                    <div className="flex items-center gap-3 mb-3">
                      <span
                        className={`px-2 py-1 text-sm font-bold rounded ${
                          response.status >= 200 && response.status < 300
                            ? "bg-green-100 text-green-700 dark:bg-green-900/30 dark:text-green-300"
                            : response.status >= 400
                            ? "bg-red-100 text-red-700 dark:bg-red-900/30 dark:text-red-300"
                            : "bg-gray-100 text-gray-700 dark:bg-gray-700 dark:text-gray-300"
                        }`}
                      >
                        {response.status}
                      </span>
                      <span className="text-sm text-gray-600 dark:text-gray-400">
                        {response.description}
                      </span>
                    </div>
                    {response.example ? (
                      <pre className="p-4 bg-gray-900 text-gray-100 rounded-lg overflow-x-auto text-sm">
                        {JSON.stringify(response.example, null, 2)}
                      </pre>
                    ) : null}
                  </div>
                ))}
              </div>
            </section>

            {/* Code Examples */}
            <section className="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 overflow-hidden">
              <div className="px-6 py-4 border-b border-gray-200 dark:border-gray-700">
                <h2 className="text-lg font-semibold text-gray-900 dark:text-white">
                  Code Examples
                </h2>
              </div>
              <div>
                <div className="border-b border-gray-200 dark:border-gray-700">
                  <nav className="flex -mb-px">
                    {(["curl", "typescript", "javascript", "python"] as CodeTab[]).map((tab) => (
                      <button
                        key={tab}
                        onClick={() => setActiveCodeTab(tab)}
                        className={`px-6 py-3 text-sm font-medium border-b-2 transition-colors ${
                          activeCodeTab === tab
                            ? "border-blue-500 text-blue-600 dark:text-blue-400"
                            : "border-transparent text-gray-500 hover:text-gray-700 dark:text-gray-400"
                        }`}
                      >
                        {tab === "curl" ? "cURL" : tab.charAt(0).toUpperCase() + tab.slice(1)}
                      </button>
                    ))}
                  </nav>
                </div>
                <div className="relative">
                  <button
                    onClick={() => copyCode(codeSnippets[activeCodeTab])}
                    className="absolute top-4 right-4 px-3 py-1 text-xs bg-gray-700 hover:bg-gray-600 text-white rounded transition-colors"
                  >
                    {copiedCode ? "Copied!" : "Copy"}
                  </button>
                  <pre className="p-6 bg-gray-900 text-gray-100 overflow-x-auto text-sm">
                    {codeSnippets[activeCodeTab]}
                  </pre>
                </div>
              </div>
            </section>
          </div>

          {/* Right Column - Tester */}
          <div className="xl:col-span-1">
            <div className="sticky top-8">
              <div className="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 overflow-hidden">
                <div className="px-6 py-4 border-b border-gray-200 dark:border-gray-700">
                  <h2 className="text-lg font-semibold text-gray-900 dark:text-white">
                    Try It Out
                  </h2>
                </div>
                <div className="p-4">
                  <ApiTester endpoint={endpoint} />
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  );
}